package com.neusoft.yuejian12.yuejian_12.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.javabean.ActivityBean;
import com.neusoft.yuejian12.yuejian_12.javabean.EnterActBean;
import com.neusoft.yuejian12.yuejian_12.javabean.UserBean;
import com.neusoft.yuejian12.yuejian_12.model.ActivityModel;
import com.neusoft.yuejian12.yuejian_12.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EnterActActivity  extends AppCompatActivity {
    //声明
    private ImageView left;
    private Button enterAct_button;
    private int u_id;
    private int a_id;
    private TextView a_name,a_price,u_name,u_phone,a_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter_act);
        a_id=getIntent().getIntExtra("a_id",-1);

        initViews();//初始化
        initEvents();//点击事件处理
        //getdata();
    }

    private void initViews() {
        a_name = (TextView) findViewById(R.id.name);

        u_name = (TextView) findViewById(R.id.u_name);
        u_phone = (TextView) findViewById(R.id.u_phone);
        a_price = (TextView) findViewById(R.id.price);

        a_address = (TextView) findViewById(R.id.a_address);
        enterAct_button = (Button) findViewById(R.id.enterAct_button);

        left= (ImageView) findViewById(R.id.left);
    }

    private void initEvents() {
        //返回上一页
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sp.getInt("u_id", 0);

        UserModel userModel = new UserModel();
        Call callu = userModel.getUserInfo(u_id);
        callu.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                UserBean user = response.body();
                u_name.setText(user.getU_name());
                u_phone.setText(user.getU_phone());
            }
            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
            }
        });


        ActivityModel activityModel = new ActivityModel();
        Call<ActivityBean> call = activityModel.getDetail(a_id);

        call.enqueue(new Callback<ActivityBean>() {
            @Override
            public void onResponse(Call<ActivityBean> call, Response<ActivityBean> response) {
                ActivityBean data = (ActivityBean) response.body();  //获取数据
                String aname = data.getA_name();
                int aprice = data.getA_price();
                String aaddress=data.getA_address();
                a_name.setText(aname);
                a_price.setText("￥"+ aprice+"");
                a_address.setText(aaddress);


            }

            @Override
            public void onFailure(Call<ActivityBean> call, Throwable t) {

            }
        });


        enterAct_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityModel activityModel = new ActivityModel();
                Call<EnterActBean> call = activityModel.addactivityn(u_id,a_id);
                call.enqueue(new Callback<EnterActBean>() {
                    @Override
                    public void onResponse(Call<EnterActBean> call, Response<EnterActBean> response) {
                        EnterActBean enterActBean=response.body();
                        if ("1".equals(enterActBean.getSuccess())) {
                            Toast.makeText(EnterActActivity.this, "报名成功", Toast.LENGTH_LONG).show();

                        }else if("2".equals(enterActBean.getSuccess())){
                            Toast.makeText(EnterActActivity.this, "请不要重复报名", Toast.LENGTH_LONG).show();
                        }
                        else {
                            Toast.makeText(EnterActActivity.this, "失败", Toast.LENGTH_LONG).show();
                        }

                    }
                    @Override
                    public void onFailure(Call<EnterActBean> call, Throwable t) {

                    }
                });
            }
        });



    }
}